package main

import (
	"fmt"
	"github.com/gocolly/colly"
	"math/rand"
	"strconv"
	"strings"
)

var waitChan chan bool

type UName struct {
	Phone map[int]string
}

var PhoneMap UName

func main() {
	//proxy := func(_ *http.Request) (*url.URL, error) {
	//	return url.Parse("http://127.0.0.1:7890")
	//}
	PhoneMap.Phone = make(map[int]string)
	waitChan = make(chan bool)
	url := "https://search.jd.com/Search?keyword=手机&wq=手机&pvid=3e540e97b43c4cbd6f17ca9c5fgjub&s=61&click=0"
	c := colly.NewCollector()
	c.SetProxy("http://127.0.0.1:7890")
	c.OnHTML("div#J_goodsList li div.p-name em", func(e *colly.HTMLElement) {
		text := e.Text
		if find := strings.Contains(text, "京品手机"); !find {
			fmt.Println(text)
			for _, v := range PhoneMap.Phone {
				if v == text {
					continue
				} else {
					fmt.Println("_____", text)
					PhoneMap.Phone[len(PhoneMap.Phone)+1] = text
				}
			}
		}
	})

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Host", "diviner.jd.com")
		r.Headers.Set("User-Agent", RandomString())
		r.Headers.Set("Referer", "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=4fd2ff1ffda34e7aa8293f129003482e")
		fmt.Println(r.URL.String())
	})
	c.OnError(func(response *colly.Response, err error) {
		fmt.Println("OnError___________________")
	})
	for i := 1; i <= 10; i = i + 2 {
		c.Visit(url + "&page=" + strconv.Itoa(i))
	}
	//c.Visit(url)
	fmt.Println(PhoneMap.Phone)
}

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func RandomString() string {
	b := make([]byte, rand.Intn(10)+10)
	for i := range b {
		b[i] = letterBytes[rand.Intn(len(letterBytes))]
	}

	return string(b)
}
